package com.edu.internship.busi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.edu.internship.busi.dto.CardListDTO;
import com.edu.internship.busi.entity.Card;
import com.edu.internship.busi.service.ICardListService;
import com.edu.internship.busi.service.ICardService;
import com.edu.internship.util.MathUtils;
import com.edu.internship.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : tianhuida
 * @version : [v1.0]
 * @createTime : [2022/8/2 9:51]
 */

@RestController
@Slf4j
@RequestMapping("api/cardList")
@Api(tags = {"卡消费的控制器"})
public class CardListController {

    @Resource
    private ICardListService cardListService;
    @Resource
    private ICardService cardService;

    @PostMapping("/create")
    @ApiOperation(value = "新增-Card", notes = "新增-Card")
    public boolean create(@Validated @RequestBody CardListDTO dto) {
        LambdaQueryWrapper<Card> cardQw = Wrappers.lambdaQuery();
        cardQw.eq(Card::getCardId,dto.getCardNo());
        List<Card> card = cardService.list(cardQw);
        Card cards = card.get(0);
        cards.setCardBalance(MathUtils.sub(card.get(0).getCardBalance(),dto.getCost()));
        cardService.updateCard(cards);

        dto.setCostTime(LocalDateTime.now());
        dto.setInTime(LocalDateTime.now());
        dto.setRemainder(cards.getCardBalance());
        dto.setStudentId(cards.getStudentId());
        dto.setId(Tools.createRandomCode(5,true));
        return cardListService.createCard(dto);
    }

}
